<!DOCTYPE html>

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 

<% 
try {
	
	int start_flag = 1;
	String connectionURL = "jdbc:mysql://localhost:3306/unilearn"; 
	
	Connection connection = null;
	Statement stmt = null;
	ResultSet rst=null;

	Class.forName("com.mysql.jdbc.Driver").newInstance(); 

	connection = DriverManager.getConnection(connectionURL, "root", "");
	stmt = connection.createStatement();
	
	
	
	if(!connection.isClosed())
	{
	
		int row_count = 0;
		
		String user_name=request.getParameter("txtUserName");
		String pw=request.getParameter("txtPassword");
			
		//out.println("user name = " + user_name);
		//out.println("Password = " + pw );
		if((user_name != null && user_name != "") && (pw != null && pw != ""))
		{ 
			rst = stmt.executeQuery("select u.*, ut.permission as permission from user u, user_type ut where u.user_type_id = ut.user_type_id and u.login_id = '" + user_name + "'and u.password = '" + pw + "'");
			
			//just get the total user from db
			while(rst.next())
			{
				//get user id store in session
				if (rst.getString("user_id") != "" && rst.getString("user_id") != null){session.setAttribute( "UserID",  rst.getString("user_id"));}
				
				//get user permission level and first name and last name
				if (rst.getString("permission") != "" && rst.getString("permission") != null){session.setAttribute( "Permission",  rst.getString("permission"));}
				if (rst.getString("last_name") != "" && rst.getString("last_name") != null){session.setAttribute( "LastName",  rst.getString("last_name"));}
				if (rst.getString("first_name") != "" && rst.getString("first_name") != null){session.setAttribute( "FirstName",  rst.getString("first_name"));}
				if (rst.getString("class_group_id") != "" && rst.getString("class_group_id") != null){session.setAttribute( "ClassGroupID",  rst.getString("class_group_id"));}
				
				row_count = row_count + 1;
			}
			
			start_flag = 0;
			rst.close();
		}
		else if((user_name != null && user_name != "") && (pw == null || pw == ""))
		{
			out.println( "<script>alert('Password must be provided')</script>" );
		}
		else if((user_name == null || user_name == "") && (pw != null && pw != "")){
			out.println( "<script>alert('User Name must be provided')</script>" );
		}
	
		//if(row_count == 1){			
		//	out.println("CheckLogin()");
		//}else
		//	out.println("Not Ok");
	//}
%>

<html lang="en">
<link rel="stylesheet" type="text/css" href="common.css">
 <head>
   <title>UniLearn System</title>
 </head>
 <body>
 <form action="Login.jsp" method="post" name="entry" onSubmit="return validate(this)">
	<table class="TblBody">
		<tr>
			<td>
				<div class="divLogin">
				    <br />
				    <br />
				    <br />
					<table width="100%">
					    <tr>
					        <td style="width:32%"></td>
					        <td style="width:50%">
					            <table class="SubTable">
						            <tr>
							            <th><span style="font-size:15px; color:#FFFFFF">Login</span></th>
						            </tr>
						            <tr>
							            <td>
								            <table width="330px">
									            <tr>
										            <td>Login: </td>
										            <td><input name="txtUserName" type="text" /></td>
								                </tr>
									            <tr>
										            <td>Password: </td>
										            <td><input name="txtPassword" type="password" /></td>
									            </tr>
									            <tr>
										            <td align="center" colspan="2">
											            <input name="btnSubmit" value="Login" type="submit" />&nbsp;&nbsp; 
														<input name="btnReset" value="Reset" type="reset" /> 
														<%
															if (row_count > 0){
																response.sendRedirect("main_page.jsp");
															}else
															{
																if(start_flag == 0)
																	out.println( "<script>alert('Wrong username or password')</script>" );
															}
															
														stmt.close();
														connection.close();
														}
														
														%>
										            </td>
									            </tr>
								            </table>
							            </td>
						            </tr>
					            </table>
					        </td>
					        <td style="width:18%"></td>
					    </tr>
					</table>
				</div>
			</td>
		</tr>
	</table>
</form>
<%
}catch(Exception ex){

	out.println(ex);
}
%>
 </body>
 </html>
